package com.luoguijia.disk.spring.util;

import com.obs.services.ObsClient;
import com.obs.services.model.ObjectListing;
import com.obs.services.model.ObsObject;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public final class ObsUtil {

    private static  final  String endpoint="obs.cn-north-4.myhuaweicloud.com";
    private static  final  String  ak = "HPUA0N2NRIUMTIXXXRDL";
    private static  final  String  sk = "wcSWlrHyjDqIlRch5HFWNvhf8AwsuPP57yClWtEm";
    private static  final  String  bucketName = "obs.cn-north-4.group2.com ";


    public static void main(String[] args) {
        ObsClient obsClient = new ObsClient(ak,sk,endpoint);
        ObjectListing list = obsClient.listObjects(bucketName);
        List<ObsObject> listObjects = list.getObjects();
        for(ObsObject obsObject:listObjects){
            System.out.println("obsKey:" + obsObject.getObjectKey());
            System.out.println("obsSize:"  + obsObject.getMetadata().getContentLength());
        }
//文件下载
    /*    ObsObject obsObject = obsClient.getObject(bucketName, "1.txt");
        InputStream is = obsObject.getObjectContent();
        byte[] block = new byte[1024];
        Integer length = 0;
        try(FileOutputStream fileOutputStream =  new FileOutputStream("D:\\logfile\\1.txt");
        ){
            while ((length =  is.read(block)) > 0 ){
                fileOutputStream.write(block,0,length);
                fileOutputStream.flush();
            }
        }catch (IOException e1){
            e1.printStackTrace();
        }*/
//文件上传
        try (FileInputStream inputStream = new FileInputStream("D:\\logfile\\a1.jpg");){
            obsClient.putObject(bucketName,"a4.jpg",inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }


        //文件删除
        String objKey = "a4.jpg";
        if(obsClient.doesObjectExist(bucketName,objKey)){
            obsClient.deleteObject(bucketName,objKey);
        }

    }


}
